/**
 * @projectName: petJoyHome
 * @author: 小飞
 * @description: TODO
 * @date: 2024/3/2 19:48
 * @version: 1.0
 */
package com.home.controller;

import com.home.dto.CreateUserDTO;
import com.home.dto.LoginDTO;
import com.home.dto.PageQueryDTO;
import com.home.result.PageResult;
import com.home.result.Result;
import com.home.service.UserService;
import com.home.vo.LoginUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/home/user/")
@Api(tags = "用户管理模块")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @ApiOperation("用户登录")
    @PostMapping("login")
    public Result<LoginUserVo> login(@Valid @RequestBody LoginDTO loginDTO){
        log.info("用户开始登录,登录信息为{}",loginDTO);
        LoginUserVo loginUserVo = userService.login(loginDTO);
        return Result.success("登陆成功",loginUserVo);
    }

    /**
     * 分页查询用户信息
     *
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("用户分页查询接口")
    @GetMapping("userPage")
    @PreAuthorize("hasAnyAuthority('club_club_commander','school_admin','college_admin','instructor')")
    public Result<PageResult> getPageUser(@Valid PageQueryDTO pageQueryDTO) {
        log.info("查询信息为:{}", pageQueryDTO);
        PageResult pageResult = userService.getPageUser(pageQueryDTO);
        return Result.success(null,pageResult);
    }

    /**
     * 新增用户
     *
     * @param createUserDTO
     * @return
     */
    @ApiOperation("新增用户接口")
    @PostMapping("createUser")
    public Result<?> createUser(@RequestBody @Valid CreateUserDTO createUserDTO) {
        log.info("新增用户信息为:{}", createUserDTO);
        userService.createUser(createUserDTO);
        return Result.success("新增成功");
    }
}
